<?php
// 假设企业号在公众平台上设置的参数如下
include_once "callback/WXBizMsgCrypt.php";
$encodingAesKey = "S6CB9goX7KGrrIK6fVe3cBrpX5rVkQEUssFTDBriQK5";
$token = "RCcTvDPohI1aP6Z2e8yxdytD9n051XjV";
$corpId = "ww28c6841644d2956c";
//设置utf-8应该是最最最关键的一步
header("Content-Type:text/html; charset=utf-8");
$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
//{"msg_signature":"2e4dfa8136e169a77669307e12695c4750d088e4","timestamp":"1558175027","nonce":"1557796602"}
$V = '<xml><ToUserName><![CDATA[ww28c6841644d2956c]]></ToUserName><Encrypt><![CDATA[rt0yKbBe+VgeU+omrf1l16uhciR7NkMJw3uKSDsqwCD8VCM8wiHWWhCisBQtGXS/YXO+MsJYWhAkMHAyoEbfhIVADs8GN8DzXjTmcWEdRdL3oGt70dehezViyTIDERelabI0xaZbmmgT2qWHU8cOexy51v6MsDszjkK1mf0z7Q9LsR53+O4hInFVu6YPtC8lfc8yzbbVgY30mhqKSwTomawYBx2lAjBv165rQq6McgBNgY2hONYSCX6/6/DUUm0KbQqXBIYxfvHcVYelamUVy5IbnLf8esGortZm2knYDlpvarIYAg8OaxfyX+JHt9KFfdCLTnyuVk4BJIqx3pOwsp9bFHe/kV+9z5ERMEZb8ndenqCuPjvtmuzsTnLRllbhL8MgbdnucdGnyKw96T8/nQC5x4J3AVkiJWACsU+pZOI=]]></Encrypt><AgentID><![CDATA[1000032]]></AgentID></xml>';
$wxcpt->DecryptMsg('2e4dfa8136e169a77669307e12695c4750d088e4','1558175027','1557796602',$V,$A);
$jsonxml = json_encode(simplexml_load_string($A, 'SimpleXMLElement', LIBXML_NOCDATA));
$jsonArr = json_decode($jsonxml,true);
$ev = $jsonArr['Event'];
$id = $jsonArr['FromUserName'];
$connect = mysqli_connect('rm-uf66r00j217i4egckzo.mysql.rds.aliyuncs.com','root2','WWWlife8','salesman','3306');

if($jsonArr['Event']=='subscribe'){//关注

    $sql = "INSERT INTO sh_crm_ceshi (var,id) 
VALUES ('$ev','$id')";
}else{//取关
    $sql = "INSERT INTO sh_crm_ceshi (var,id) 
VALUES ('$ev','$id')";
}
mysqli_query($connect,'SET NAMES UTF8');
mysqli_query($connect,$sql);

var_dump($jsonArr);
